# 执行celery的任务函数
from celery_tasks.celery_main import app
from config.dbs import emailConfig
from django.core.mail import send_mail
import logging
logger = logging.getLogger(__name__)


@app.task(name='send_email')
def send_email(email, html_message):
    # 尝试进行发送邮件
    try:
        res = send_mail(
            subject='jutong32商场订单',
            message='',
            from_email=emailConfig.EMAIL_HOST_USER,
            recipient_list=[email],
            html_message=html_message
        )
    # 发送邮件失败
    except Exception as e:
        logger.error(f'邮件发送异常,异常为{e}')
    else:
        # 判断res是否为1,为1则发送成功过
        if res:
            logger.info(f'邮件发送成功,收件人为{email}')
        else:
            logger.warning(f'邮件发送失败,收件人为{email}')
